package com.hnucm.cooklife.custom;

import com.google.android.material.appbar.AppBarLayout;

public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    enum State{
       EXPANDED,COLLAPSED,IDLE;
   }

    private State mCurrentState = State.IDLE;

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if(verticalOffset == 0){
            if(mCurrentState != State.EXPANDED){
                onStateChanged(appBarLayout,State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if(appBarLayout != null){
            if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){
                if(mCurrentState != State.COLLAPSED){
                    onStateChanged(appBarLayout,State.COLLAPSED);
                }
                mCurrentState = State.COLLAPSED;
            } else {
                if(mCurrentState != State.IDLE){
                    onStateChanged(appBarLayout,State.IDLE);
                }
                mCurrentState = State.IDLE;
            }
        }
    }

    abstract void onStateChanged(AppBarLayout appBarLayout,
                                 State state);
}
